package com.xhx.spring;

import com.xhx.spring.config.AppConfig;
import com.xhx.spring.cycledepend.AService;
import com.xhx.spring.service.BeanLifeService;
import com.xhx.spring.service.GoodsService;
import com.xhx.spring.service.UserService;
import com.xhx.spring.service.UserServiceImpl;
import com.xhx.spring.service.postprocess.MyBean;
import com.xhx.spring.service.postprocess.MyBeanPostProcessor;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2023-03-02 22:08
 **/
public class SpringTest {
    // new UserServiceImpl --> 无参构造函数 --> 对象--> 依赖注入 --> 放入单例池 --> bean
    // new UserServiceImpl --> 推断构造函数 --> 普通对象 --> 依赖注入  --> 初始化前(@PostConstruct)
    //      --> 初始化(InitializingBean) --> 初始化后(AOP) --> 代理对象--> 放入单例池 --> bean

    //AOP(cglib): 先生成代理对象proxy,proxy继承当前对象，代理对象有个属性是原普通对象。

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
        userService.save();


        UserService userService1 = new UserServiceImpl();
//        for (Field field : userService1.getClass().getDeclaredFields()) {
//            if (field.isAnnotationPresent(Autowired.class)) {
//                field.set(userService1, ???);
//            }
//        }

//        for (Method method : userService1.getClass().getMethods()) {
//            if (method.isAnnotationPresent(PostConstruct.class)) {
//                method.invoke(userService1,???);
//            }
//        }

    }

    @Test
    public void test2() {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService1 = (UserService) applicationContext.getBean("userServiceImpl");
        UserService userService2 = (UserService) applicationContext.getBean("userService2");
        UserService userService3 = (UserService) applicationContext.getBean("userService3");
        System.out.println(userService1);
        System.out.println(userService2);
        System.out.println(userService3);
    }

    @Test
    public void test3() {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        GoodsService goodsService = (GoodsService) applicationContext.getBean("goodsService");
        goodsService.slect();
    }


    @Test
    public void test4() {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        GoodsService goodsService = (GoodsService) applicationContext.getBean("goodsService");

        goodsService.saveGood();
    }

    @Test
    public void test5() throws Exception {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        BeanLifeService beanLifeService = (BeanLifeService) applicationContext.getBean("beanLifeService");
//        beanLifeService.destroy();
        applicationContext.close();
    }

    @Test
    public void test6() throws Exception {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        MyBeanPostProcessor myBeanPostProcessor =
                (MyBeanPostProcessor) applicationContext.getBean("myBeanPostProcessor");
//        beanLifeService.destroy();
    }

    @Test
    public void test7() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        MyBean myBean = (MyBean) applicationContext.getBean("myBean");
        applicationContext.close();
    }

    @Test
    public void test8() {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
//        UserService userService1 = (UserService) applicationContext.getBean("userService");
//        UserService userService2 = (UserService) applicationContext.getBean("userService");
//        UserService userService3 = (UserService) applicationContext.getBean("userService");
//        System.out.println(userService1);
//        System.out.println(userService2);
//        System.out.println(userService3);
    }


    @Test
    public void test9() {
        AnnotationConfigApplicationContext applicationContext
                = new AnnotationConfigApplicationContext(AppConfig.class);
        AService aService = (AService) applicationContext.getBean("AService");
        aService.log();

    }
}
